using System.Collections;
using System.Collections.Generic;
using System.IO;
using HotUpdate.Config;
using Newtonsoft.Json;
using UnityEngine;

namespace HotUpdate.Persistence
{
    /// <summary>
    /// 持久化存储
    /// </summary>
    public class PersistenceMgr
    {
        public void SavePlayerData<T>(string fileName, T data)
        {
            var jsonStr = JsonConvert.SerializeObject(data);
            var bytes = System.Text.Encoding.UTF8.GetBytes(jsonStr);
            File.WriteAllBytes(ProjectCfg.PLAYER_DATA_PATH + fileName, bytes);
        }

        public T LoadPlayerData<T>(string fileName, T data)
        {
            if (File.Exists(ProjectCfg.PLAYER_DATA_PATH + fileName))
            {
                var bytes = File.ReadAllBytes(ProjectCfg.PLAYER_DATA_PATH + fileName);
                data = JsonConvert.DeserializeObject<T>(System.Text.Encoding.UTF8.GetString(bytes));
                return data;
            }
            return default;
        }
    }
}